package com.aracky.common.util;

import com.aracky.common.vo.LoginUserVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.List;


/**
 * @author zengzhiping
 * @date 2022/04/08 18:28
 **/
public class UserIPUtil {

    /**
     * 判断用户是否登录
     *
     * @return
     */
    public static Boolean getUserIsLoginIn() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null && !"anonymousUser".equals(authentication.getName())){
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    //获取当前用户的ID
    public static Long getCurrentUserId() {
        return ((LoginUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserId();
    }

    //获取当前用户的登录名
    public static String getCurrentUserName() {
        return ((LoginUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    }

    //获取当前用户的用户姓名
    public static String getCurrentUserRealName() {
        return ((LoginUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    }

    //获取角色集合
    public static List<String> getCurrentRoleKey() {
        return ((LoginUserVO) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getRoleList();
    }
}
